package com.example.module_motorcircle.report.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_net.model.api.ReportTypeModel
import com.example.module_motorcircle.databinding.LayoutItemReportTypeBinding

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/14
 */
class ReportTypeAdapter(
    private val items:List<ReportTypeModel>,
    private val onCheckItem:((ReportTypeModel)->Unit)? = null
): RecyclerView.Adapter<ReportTypeAdapter.ReportTypeViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReportTypeViewHolder {
        val binding = LayoutItemReportTypeBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ReportTypeViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    override fun onBindViewHolder(holder: ReportTypeViewHolder, position: Int) {
        holder.binding.reportTypeModel = items[position]
        //item点击
        holder.binding.root.setOnClickListener {
            if(items[position].isChecked) return@setOnClickListener
            //重置其他item为false
            for (i in items.indices) {
                if(items[i].isChecked){
                    items[i].isChecked = false
                    notifyItemChanged(i)
                }
            }
            //设置当前为true
            items[position].isChecked = true
            onCheckItem?.invoke(items[position])
            notifyItemChanged(position)
        }



    }
    inner class ReportTypeViewHolder(val binding:LayoutItemReportTypeBinding):RecyclerView.ViewHolder(binding.root){

    }
}